package com.harmonyos.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.harmonyos.domain.Limit;
import com.harmonyos.domain.TbFourm;
import com.harmonyos.domain.TbTz;
import com.harmonyos.domain.TbUser;
import com.harmonyos.mapper.TbFourmMapper;
import com.harmonyos.mapper.TbTzMapper;
import com.harmonyos.service.TbUserService;
import com.harmonyos.mapper.TbUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
        implements TbUserService {

    @Resource
    private TbUserMapper tbUserMapper;
    @Resource
    private TbFourmMapper tbFourmMapper;
    @Resource
    private TbTzMapper tbTzMapper;

    @Override
    public String insert(TbUser user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbUserMapper.insert(user);
            objectObjectHashMap.put("code", "200");
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String select(String user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbUser user1 = JSONUtil.toBean(user, TbUser.class);
            TbUser tbUser = tbUserMapper.selectById(user1.getUser());
            objectObjectHashMap.put("data", tbUser);
            objectObjectHashMap.put("code", "200");
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);

    }

    @Override
    public String update(TbUser user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            tbUserMapper.updateById(user);
            objectObjectHashMap.put("code", "200");
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String login(String json) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbUser user1 = JSONUtil.toBean(json, TbUser.class);
            QueryWrapper<TbUser> query = new QueryWrapper<>();
            query.eq("user", user1.getUser()).eq("type", user1.getType());
            TbUser tbUser = tbUserMapper.selectOne(query);
            objectObjectHashMap.put("data", tbUser);
            objectObjectHashMap.put("code", "200");
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectUserLimit(String json) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            Limit<JSONObject> limit = JSONUtil.toBean(json, Limit.class);
            JSONObject jsonObject = limit.getData();
            TbUser tbUser = JSONUtil.toBean(jsonObject, TbUser.class);
            PageHelper.startPage(limit.getCurrent(),limit.getSize());
            QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
            if (!tbUser.getUser().equals("")) {
                queryWrapper.eq("user", tbUser.getUser());
            }
            if (!tbUser.getPhones().equals("")) {
                queryWrapper.eq("phones", tbUser.getPhones());
            }
            if (tbUser.getType() != -1) {
                queryWrapper.eq("type", tbUser.getType());
            }
            List<TbUser> tbUsers = tbUserMapper.selectList(queryWrapper);
            PageInfo<TbUser> info=new PageInfo<>(tbUsers);
            List<TbUser> userList=info.getList();

            objectObjectHashMap.put("data", userList);
            objectObjectHashMap.put("code", "200");
            objectObjectHashMap.put("total", info.getTotal());
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String selectByUser(String json) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            TbUser user1 = JSONUtil.toBean(json, TbUser.class);
            QueryWrapper<TbUser> query = new QueryWrapper<>();
            List<TbUser> tbUsers = new ArrayList<>();
            if ((user1.getUser().equals("") && user1.getPhones().equals(""))) {
                tbUsers = tbUserMapper.selectList(null);
            } else {
                if (!user1.getUser().equals("")) {
                }
                {
                    query.eq("user", user1.getUser());
                }
                if (!user1.getPhones().equals("")) {
                    query.eq("phones", user1.getPhones());
                }
                tbUsers = tbUserMapper.selectList(query);
            }
            objectObjectHashMap.put("data", tbUsers);
            objectObjectHashMap.put("code", "200");
        } catch (Exception e) {
            objectObjectHashMap.put("code", "500");
            e.printStackTrace();
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }

    @Override
    public String deleteUser(String user) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        try {
            QueryWrapper<TbUser> query = new QueryWrapper<>();
            query.eq("user",user);
            QueryWrapper<TbTz> queryTz = new QueryWrapper<>();
            queryTz.eq("byuser",user);
            QueryWrapper<TbFourm> queryFourm = new QueryWrapper<>();
            queryFourm.eq("user",user);
            tbUserMapper.delete(query);
            tbTzMapper.delete(queryTz);
            tbFourmMapper.delete(queryFourm);
            objectObjectHashMap.put("code", "200");
        }catch (Exception e){
            objectObjectHashMap.put("code", "500");
        }
        return JSONUtil.toJsonStr(objectObjectHashMap);
    }
}




